﻿using OnlineCourse.Model;
using OnlineCourse.Model.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineCourse.Services
{
    public class TeacherService : IServices.ITeacherServices
    {
        private readonly ApplicationDbContext _db;
        public TeacherService(ApplicationDbContext db)
        {
            this._db = db;
        }
        /// <summary>
        /// 添加老师信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="headImage"></param>
        /// <param name="intro"></param>
        /// <returns></returns>
        public bool Add(string name, string headImage, string intro)
        {
            try
            {
                Teacher model = new Teacher();
                model.Name = name;
                model.HeadImage = headImage;
                model.Intro = intro;
                _db.Teachers.Add(model);
                _db.SaveChanges();
                return true;
            }
            catch (Exception err)
            {
                throw err;
            }
        }
        /// <summary>
        /// 更改老师信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="headImage"></param>
        /// <param name="intro"></param>
        /// <returns></returns>
        public bool Update(Guid id, string name, string headImage, string intro)
        {
            try
            {
                var m = _db.Teachers.FirstOrDefault(p => p.Id == id);
                if(id!=null)
                {
                    m.Name = name;
                    m.HeadImage = headImage;
                    m.Intro = intro;
                    _db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 按地址查询老师信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Teacher> GetList(Guid id)
        {
            var list = _db.Teachers.FirstOrDefault (p=>p.Id ==id); 
            var a = _db.Teachers.Where(p => p.Id == list.Id);
            return a.ToList ();
        }
        /// <summary>
        /// 删除老师信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(Guid id)
        {
            try
            {
                var m = _db.Teachers.FirstOrDefault(p => p.Id == id);
                _db.Teachers.Remove(m);
                _db.SaveChanges();
                return true;
            }
            catch (Exception err)
            {
                throw err;
            }
        }
    }
}
